package com.effective;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Age implements Comparable<Age> {

	private Integer age;

	public Age(Integer age) {
		super();
		this.age = age;
	}

	@Override
	public int compareTo(Age age) {
		return this.age - age.age;
	}

	@Override
	public String toString() {
		return "Age [age=" + age + "]";
	}

	public static <T extends Comparable<? super T>> T max(List<? extends T> list) {
		Iterator<? extends T> iterator = list.iterator();
		T result = iterator.next();
		while (iterator.hasNext()) {
			T next = iterator.next();
			if (result.compareTo(next) < 0) {
				result = next;
			}
		}
		return result;
	}

	static Annotation getAnnotation(AnnotatedElement element, String annotationTypeName) {
		Class<?> annotationType = null; // Unbounded type token
		try {
			annotationType = Class.forName(annotationTypeName);
		} catch (Exception ex) {
			throw new IllegalArgumentException(ex);
		}
		return element.getAnnotation(annotationType.asSubclass(Annotation.class));
	}
	
	public static final int APPLE_FUJI = 0;
	public static final int APPLE_PIPPIN = 1;
	public static final int APPLE_GRANNY_SMITH = 2;
	public static final int ORANGE_NAVEL = 0;
	public static final int ORANGE_TEMPLE = 1;
	public static final int ORANGE_BLOOD = 2;

	public static void main(String[] args) {
		Age a1 = new Age(1);
		Age a3 = new Age(3);
		Age a2 = new Age(2);
		AgeSub aa4 = new AgeSub(4);
		AgeSub aa5 = new AgeSub(5);
		AgeSub aa6 = new AgeSub(6);
		List<Age> list = new ArrayList<Age>();
		list.add(a1);
		list.add(a3);
		list.add(a2);
		list.add(aa4);
		list.add(aa6);
		list.add(aa5);
		System.out.println(max(list));
		
		
	}
}
